<template>
  <div id="goods_id">
    <div class="goods_top">
      <div class="goods_add">
        <el-button size="mini" round @click="isShow.flag = true"
          >增加</el-button
        >
      </div>
    </div>
    <el-table :data="goods_list" style="width: 100%; text-align: center">
      <el-table-column
        prop="id"
        label="ID"
        fixed
        width="150px"
      ></el-table-column>
      <el-table-column prop="firstcatename" label="一级分类" width="150px">
      </el-table-column>
      <el-table-column
        prop="secondcatename"
        label="二级分类"
        width="150px"
      ></el-table-column>
      <el-table-column
        prop="goodsname"
        label="商品名称"
        width="150px"
      ></el-table-column>
      <el-table-column
        prop="img"
        label="类别图片"
        v-slot="$props"
        width="150px"
      >
        <template>
          <img
            :src="static_host + $props.row.img"
            alt=""
            class="pic"
            v-if="$props.row.pid != 0"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="price"
        label="零售价格"
        width="150px"
      ></el-table-column>
      <el-table-column
        prop="market_price"
        label="市场价格"
        width="150px"
        v-slot="$prop"
      ><temtlate>
        <span style="color:red">￥{{$prop.row.market_price}}</span>
        </temtlate></el-table-column>
      <el-table-column
        prop="isnew"
        label="是否新品"
        v-slot="$props"
        width="150px"
      >
        <template>
          <el-switch
            v-model="$props.row.isnew"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="ishot"
        label="是否热卖"
        v-slot="$props"
        width="150px"
      >
        <template>
          <el-switch
            v-model="$props.row.ishot"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" v-slot="$props" width="150px">
        <template>
          <el-button
            v-if="$props.row.status === 0"
            size="mini"
            type="danger"
            plain
            round
            >禁用</el-button
          >
          <el-button
            v-else-if="$props.row.status === 1"
            size="mini"
            type="success"
            plain
            round
            >正常</el-button
          >
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200px" v-slot="props">
        <template>
          <el-button size="mini" type="warning" @click="upload_goods(props.row)"
            >修改</el-button
          >
          <el-button size="mini" type="danger" @click="delGoods(props.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="count"
      :page-size="page_size"
      prev-text="上一页"
      next-text="下一页"
      @current-change="change_page"
    >
    </el-pagination>
    <AddGoods
      :isShow="isShow"
      :goods_list="goods_list"
      ref="add_goods_ref"
      @reload_goods="reload_goods"
    ></AddGoods>
  </div>
</template>

<script>
import AddGoods from "../components/addGoods.vue";
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      isShow: { flag: false },
      page: 1,
      page_size: 5,
      total: 0,
    };
  },
  created() {
    this.GoodsActions({ page: this.page, size: this.page_size });
    this.GoodsCountActions();
  },
  methods: {
    Del(id) {
      var url = "/api/goodsdelete";
      this.$http.post(url, { id }).then((res) => {
        if (res.code === 200) {
          this.GoodsActions();
          this.GoodsCountActions();
        }
      });
    },
    delGoods(id) {
      this.$confirm("确定要删除角色吗？", "删除角色", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          this.Del(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    upload_goods(obj) {
      this.isShow.flag = true;
      delete obj.firstcatename;
      delete obj.secondcatename;

      this.$refs.add_goods_ref.setData(obj);
    },
    reload_goods() {
      this.GoodsActions();
      this.GoodsCountActions();
    },
    change_page(page) {
      this.page = page;
      this.GoodsActions({ page: this.page, size: this.page_size });
    },
    ...mapActions("GoodsStore", ["GoodsActions", "GoodsCountActions"]),
  },
  computed: {
    ...mapState({
      goods_list: (state) => state.GoodsStore.goods_list,
      count: (state) => state.GoodsStore.count,
    }),
  },
  components: {
    AddGoods,
  },
};
</script>

<style>
#goods_id .goods_top {
  display: flex;
  justify-content: space-between;
  flex-direction: row-reverse;
}
#goods_id .el-table {
  margin-top: 20px;
}
#goods_id .goods_add > button {
  width: 80px;
  color: #ff5d3e;
  background-color: white;
}
#goods_id .pic {
  width: 100px;
}
#goods_id .red {
  color: red;
}
</style>